// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "StiffnessMatrixRepository.h"

#include "DBManager/Repository/HDF5/Base/HDF5RepositoryMacro.h"

HDF5_REG_NO_SORT_REPOSITORY (StiffnessMatrixRepository)

using namespace SG::DBManager::HDF5;
using namespace SG::DBManager;
using namespace SG::DataStructure::Common;

StiffnessMatrixRepository::StiffnessMatrixRepository (SG::DBManager::HDF5Database& database) : RepositoryBase (database)
{}

void StiffnessMatrixRepository::InitialDatasetInfo ()
{}

Status StiffnessMatrixRepository::Add (SG::DataStructure::FEM::ElementTypes etype, std::vector<Real>& data)
{
    const std::string currentPath = m_path + std::to_string (etype);

    // 先获取dataset 如果没有就创建
    if (!GetDatabase ().GetFile ()->exist (currentPath))
    {
        auto dataset = GetDatabase ().GetFile ()->createDataSet (currentPath,
                                                                 HighFive::DataSpace ({ data.size () }),
                                                                 HighFive::AtomicType<Real> ());  ///< 创建数据集

        // 然后覆盖保存
        dataset.write (data);
    }
    else
    {
        // 然后覆盖保存
        GetDataset (currentPath).write (data);
    }

    return Status::STATUS_Success;
}

Status StiffnessMatrixRepository::Get (SG::DataStructure::FEM::ElementTypes etype, std::vector<Real>& _OUT data)
{
    std::string const currentPath = m_path + std::to_string (etype);

    // 先获取dataset 如果没有就创建
    if (!GetDatabase ().GetFile ()->exist (currentPath))
    {
        return Status::STATUS_Failure;
    }

    GetDataset (currentPath).read (data);

    return Status::STATUS_Success;
}
